package com.css.dzj.dzjzhfx.modules.education;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjzhfx.common.util.HttpClientUtil;
import com.css.dzj.dzjzhfx.common.util.R;
import com.css.dzj.dzjzhfx.common.util.ServiceUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/education")
public class EducationByYear {

    @Value("${xcjyURL}")
    private String xcjyURL;

    @GetMapping("/mapByYear")
    private R mapByYear(String year, HttpServletRequest request) {
        try {
            String[] mapPro=new String[]{"南海诸岛","北京","天津","上海","重庆", "河北","河南","云南", "辽宁","黑龙江", "湖南", "安徽","山东","新疆","江苏", "浙江","江西","湖北","广西","甘肃","山西","内蒙古","陕西","吉林","福建","贵州","广东","青海","西藏","四川","宁夏","海南","台湾","香港","澳门"};
            String orgName = ServiceUtil.getOrgName(request);
            orgName = orgName.replace("地震局", "");
            String msg = HttpClientUtil.doGet(xcjyURL + "/material/mapCountByYearAndOrgName?year=" + year + "&orgName=" + orgName);
            Map<String, Object> map = new HashMap<>();
            if(!StringUtils.isEmpty(msg)) {
                JSONObject jsonObject = JSON.parseObject(msg);
                Object count = jsonObject.get("data");
                map = JSONObject.parseObject(count.toString());
            }

            List<Map<String, Object>> newList = new ArrayList<>();
            Integer num = 0;
            for(String arr:mapPro){
                HashMap mapNew=new HashMap();
                if(map.containsKey(arr)) {
                    Integer value = (Integer) map.get(arr);
                    mapNew.put("name", arr);
                    mapNew.put("value", value);
                    num += value;
                } else {
                    mapNew.put("name", arr);
                    mapNew.put("value", 0);
                }
                newList.add(mapNew);
            }
            String dataUnit=JSON.toJSONString(newList);

            return R.ok().put("data", dataUnit).put("cityTotal",num);
        } catch (Exception e) {

            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/getbaryear")
    private R getbaryear(String year, HttpServletRequest request) {
        try {
            String orgName = ServiceUtil.getOrgName(request);
            if(StringUtils.isEmpty(orgName)) {
                return R.error("所属机构不能为空！");
            }
            orgName = orgName.replace("地震局", "");
            String msg = HttpClientUtil.doGet(xcjyURL + "/material/countSpByYearAndOrgName?year=" + year + "&orgName=" + orgName);
            JSONObject jsonObject = JSON.parseObject(msg);
            Object count = jsonObject.get("data");
            Map<String, Object> map = JSONObject.parseObject(count.toString());
            List<Map<String, Object>> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                HashMap<String, Object> newMap = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();

                newMap.put("name", name);
                newMap.put("value", value);
                list.add(newMap);
            }
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    @GetMapping("/getpieyear")
    private R getpieyear(String year, HttpServletRequest request) {
        try {
            String orgName = ServiceUtil.getOrgName(request);
            if(StringUtils.isEmpty(orgName)) {
                return R.error("所属机构不能为空！");
            }
            orgName = orgName.replace("地震局", "");
            String msg = HttpClientUtil.doGet(xcjyURL + "/material/countSpByYearAndOrgName?year=" + year + "&orgName=" + orgName);
            JSONObject jsonObject = JSON.parseObject(msg);
            Object count = jsonObject.get("data");
            Map<String, Object> map = JSONObject.parseObject(count.toString());
            List<Map<String, Object>> list = new ArrayList<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                HashMap<String, Object> newMap = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();

                newMap.put("name", name);
                newMap.put("value", value);
                list.add(newMap);
            }
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


}
